package com.dyn.videohub.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyn.videohub.Common.R;
import com.dyn.videohub.Service.VideoService;
import com.dyn.videohub.domain.Video;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RestController
@Api(tags = "视频管理", value = "视频接口")
@RequestMapping("/video")
public class VideoController {
    @Resource
    private VideoService videoService;

    /***
     * 获取所有视频
     * @return
     */
    @ApiOperation(value = "获取所有视频")
    @GetMapping("/getAll")
    public List<Video> getAll() {
        return videoService.list();
    }

    /***
     * 关键字搜索视频
     * @param key 关键字
     * @return
     */
    @ApiOperation(value = "关键字搜索视频")
    @GetMapping("/searchVideo")
    public List<Video> searchVideo(@RequestParam("key") String key) {
        LambdaQueryWrapper<Video> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Video::getName, key).or().like(Video::getLabel, key);
        List<Video> videoList = videoService.list(lambdaQueryWrapper);
        return videoList;
    }

    /***
     * 分享码查找视频
     * @param Key
     * @return
     */
    @ApiOperation(value = "分享码搜索视频")
    @GetMapping("/numberVideo")
    public R<Video> getVideo(@RequestParam("keyNumber") Long Key) {
        LambdaQueryWrapper<Video> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //通过id码查找视频，类似B站分享码
        lambdaQueryWrapper.eq(Video::getVideoId, Key);
        Video video = videoService.getOne(lambdaQueryWrapper);
        return R.success(video);
    }

    /***
     * 获取需要播放的视频
     * @param videoId
     * @return
     */
    @ApiOperation(value = "获取播放视频")
    @GetMapping("/playVideo")
    public R<Video> playVideo(@RequestParam("videoId") Long videoId) {
        Video video = videoService.getById(videoId);
        return R.success(video);
    }

}
